package com.thug.ai;

import java.util.ArrayList;

import org.anddev.andengine.entity.modifier.PathModifier.Path;

import com.badlogic.gdx.math.Vector2;
import com.thug.engine.Utility;
import com.thug.entities.Bot;
import com.thug.entities.Bot.BotStates;

public class PlayerFollowAI extends AIManager<Bot> {


	private final static PlayerFollowAI mInstance = new PlayerFollowAI();

	private PlayerFollowAI() {
	}

	public final static PlayerFollowAI getInstance() { return mInstance; }

	@Override
	public void update(float elapsedTime) {
		for(Bot bot : mEntities) {
			update(bot, elapsedTime);
		}
	}

	private void update(Bot bot, float elapsedTime) {
		if(bot.isActive()) {
			if(bot.isStateStackEmpty()) {
				bot.pushState(BotStates.BS_SEEK);
				ArrayList<Vector2> points = mStageMap.findPath(new Vector2(bot.getX(), bot.getY()), new Vector2(mPlayerEntity.getX(), mPlayerEntity.getY()));
				if(points != null) {
					Path p = Utility.genReversePath(points, -bot.getSprite().getBaseWidth() * 0.5f, -bot.getSprite().getBaseHeight() * 0.5f);
					bot.startFollowing(p, p.getSize() / 2);
					points.clear();
				}
			}
			else if(bot.isPathFinished()) {
				bot.setActive(false);
				bot.popState();
			}
		}
	}



}
